﻿// Decompiled with JetBrains decompiler
// Type: MCM.Abstractions.Wrapper.SettingsPropertyGroupDefinitionWrapper
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using HarmonyLib.BUTR.Extensions;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

#nullable enable
namespace MCM.Abstractions.Wrapper
{
  public sealed class SettingsPropertyGroupDefinitionWrapper : SettingsPropertyGroupDefinition
  {
    private static string? GetGroupName(object @object)
    {
      return @object.GetType().GetProperty("GroupName")?.GetValue(@object) as string;
    }

    private static string? GetGroupNameOverride(object @object)
    {
      return @object.GetType().GetProperty("GroupNameOverride")?.GetValue(@object) as string;
    }

    private static int? GetGroupOrder(object @object)
    {
      return @object.GetType().GetProperty("Order")?.GetValue(@object) as int?;
    }

    private static char? GetSubGroupDelimiter(object @object)
    {
      return @object.GetType().GetProperty("SubGroupDelimiter")?.GetValue(@object) as char?;
    }

    public SettingsPropertyGroupDefinitionWrapper(object @object)
      : base(SettingsPropertyGroupDefinitionWrapper.GetGroupName(@object) ?? "ERROR", SettingsPropertyGroupDefinitionWrapper.GetGroupOrder(@object).GetValueOrDefault(-1))
    {
      this.SetSubGroupDelimiter(SettingsPropertyGroupDefinitionWrapper.GetSubGroupDelimiter(@object).GetValueOrDefault('/'));
      this.subGroups.AddRange((IEnumerable<SettingsPropertyGroupDefinition>) SettingsPropertyGroupDefinitionWrapper.GetSubGroups(@object).SortDefault());
      this.settingProperties.AddRange((IEnumerable<ISettingsPropertyDefinition>) SettingsPropertyGroupDefinitionWrapper.GetSettingProperties(@object).SortDefault());
    }

    private static IEnumerable<SettingsPropertyGroupDefinition> GetSubGroups(object @object)
    {
      PropertyInfo propertyInfo = AccessTools2.Property(@object.GetType(), "SubGroups");
      if ((object) propertyInfo == null)
        propertyInfo = AccessTools2.Property(@object.GetType(), "SettingPropertyGroups");
      PropertyInfo subGroupsProperty = propertyInfo;
      if (subGroupsProperty?.GetValue(@object) is IEnumerable list)
      {
        foreach (object obj in list)
          yield return (SettingsPropertyGroupDefinition) new SettingsPropertyGroupDefinitionWrapper(obj);
      }
    }

    private static IEnumerable<ISettingsPropertyDefinition> GetSettingProperties(object @object)
    {
      PropertyInfo settingPropertiesProperty = AccessTools2.Property(@object.GetType(), "SettingProperties");
      if (settingPropertiesProperty?.GetValue(@object) is IEnumerable list)
      {
        foreach (object obj in list)
          yield return (ISettingsPropertyDefinition) new SettingsPropertyDefinitionWrapper(obj);
      }
    }
  }
}
